package ads.corporativa.aula0204.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConexaoBd{
	
	private static ConexaoBd conexao;
	
	public static ConexaoBd getInstance(){
		if(conexao==null){
		   conexao = new ConexaoBd();
		}
		
		return conexao;
	}
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException{
		
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/bdparticipantes","root" ,"root");
	}

	public void listar() throws SQLException {
		Connection connection = null;
		
		try{
			connection = getConnection();
			
			String sql = " Select * from participantes";
			
			PreparedStatement statement = connection.prepareStatement(sql);
			ResultSet resutSet = statement.executeQuery();
			
			while(resutSet.next()){
			System.out.println(resutSet.getInt("id"));
			System.out.println(resutSet.getString("cpf"));
			System.out.println(resutSet.getString("nome"));
			
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			connection.close();
			
		}
	}
		public static void main(String[] args){
			
			try {
				getInstance().listar();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
